package cn.mumzone.pos.util.returngoods;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ReturnGoodsEntity;
import cn.mumzone.pos.ui.widgets.StepView;

/**
 * Created by JoeLjt on 2019/7/13.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class AddReturnCartUtil {

    /**
     * 更新本地购物车列表
     * <p>
     * 相同商品加数量，不同商品加条目
     */
    public static void updateLocalCartList(LinkedList<ReturnGoodsEntity> localList,
                                           List<ReturnGoodsEntity> newList) {

        outerLoop:
        for (int i = 0; i < newList.size(); i++) {
            ReturnGoodsEntity recentAddGoodsBean = newList.get(i);
            for (int j = 0; j < localList.size(); j++) {
                ReturnGoodsEntity localCartBean = localList.get(j);

                // 获取到购物车列表后，与内存中列表数据进行 skuId 匹配
                // 如果是同一个商品，更新商品数量和寄存数量
                if (recentAddGoodsBean.getGoodsSkuId().equals(localCartBean.getGoodsSkuId())) {

                    // 优先给 unRegisterCount 增加数量，unRegisterCount 的上限是 (goodsNum - consignNum + sendNum)
                    // 如果 unRegisterCount 达到上限了，那就给 registerCount 增加数量，上限为 consignNum
                    int goodsUnRegisterLimit = localCartBean.getGoodsNum() - localCartBean.getConsignNum() + localCartBean.getSendNum();
                    if (localCartBean.getSelectUnRegisterCount() < goodsUnRegisterLimit) {
                        localCartBean.setSelectUnRegisterCount(localCartBean.getSelectUnRegisterCount() + 1);
                    } else {
                        localCartBean.setSelectRegisterCount(localCartBean.getSelectRegisterCount() + 1);
                    }

                    // 商品有可能寄存，选中数量由「未寄存」+「寄存」共同组成
                    localCartBean.setSelectCount(localCartBean.getSelectRegisterCount() + localCartBean.getSelectUnRegisterCount());

                    continue outerLoop;
                }

            }

            // 为新插入的数据设置选中数量，默认为 0，设为 1
            recentAddGoodsBean.setSelectCount(1);

            // 根据商品寄存情况，设置默认选中：判断 -> 寄存数量 - 领取数量 == 购买数量
            // 1. 相等意味着顾客是购买后全部寄存，并且没有做过领取，退货时则默认退寄存商品
            // 2. 不相等意味着顾客要么是未寄存，要么是部分寄存，要么是全部寄存后做过领取，那么退货时默认退未寄存商品
            if (recentAddGoodsBean.getConsignNum() - recentAddGoodsBean.getSendNum() == recentAddGoodsBean.getGoodsNum()) {
                recentAddGoodsBean.setSelectRegisterCount(1);
            } else {
                recentAddGoodsBean.setSelectUnRegisterCount(1);
            }

            // 如果有新数据，则插入链表最后
            localList.addFirst(recentAddGoodsBean);
        }

    }


    public static void updateGoodsSelectStatus(CommonAdapter<ReturnGoodsEntity> goodsAdapter,
                                               CommonAdapter<ReturnGoodsEntity> cartAdapter) {
        for (ReturnGoodsEntity entity : goodsAdapter.getDataList()) {
            entity.setSelectCount(0);
            for (ReturnGoodsEntity cartEntity : cartAdapter.getDataList()) {
                if (cartEntity.getGoodsSkuId().equals(entity.getGoodsSkuId())) {
                    entity.setSelectCount(cartEntity.getSelectCount());
                    break;
                }
            }
        }
        goodsAdapter.notifyDataSetChanged();
        cartAdapter.notifyDataSetChanged();
    }


    /**
     * 当前添加的商品是否超额
     * <p>
     * 当前要添加的商品数量 + 购物车中现有的该商品数量 <= 购买数量
     *
     * @param goodsEntity
     */
    public static boolean isGoodEnoughToReturn(List<ReturnGoodsEntity> localList, ReturnGoodsEntity goodsEntity) {
        int goodAmountLimit = goodsEntity.getGoodsNum();
        int goodsCount = goodsEntity.getSelectCount();
        int goodsCountInCart = 0;

        for (ReturnGoodsEntity cartEntity : localList) {
            if (goodsEntity.getGoodsSkuId().equals(cartEntity.getGoodsSkuId())) {
                goodsCountInCart += cartEntity.getSelectCount();
            }
        }
        return (goodsCount + goodsCountInCart) <= goodAmountLimit;
    }

}
